package com.example.tian.securityinspection.ui;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;

import com.example.tian.securityinspection.R;
import com.google.common.base.Preconditions;

/**/
public class Display {

    public static final String PARAM_ID = "_id";

    public static final String PARAM_OBJ = "_obj";

    private final AppCompatActivity mActivity;

    public Display(AppCompatActivity activity){
        mActivity = Preconditions.checkNotNull(activity, "activity cannot be null");
    }

    /**
     * 设置标题
     * @param title
     */
    public void setActionBarTitle(CharSequence title){
        ActionBar ab = mActivity.getSupportActionBar();
        if (ab != null){
            ab.setTitle(title);
        }
    }

    /**
     * 设置app bar 子标题
     * @param title
     */
    public void setActionBarSubtitle(CharSequence title){
        ActionBar ab = mActivity.getSupportActionBar();
        if (ab != null){
            ab.setSubtitle(title);
        }
    }

    /**
     * 设置是否显示回退按钮
     * @param isShow
     */
    public void showUpNavigation(boolean isShow){
        final ActionBar ab = mActivity.getSupportActionBar();
        if (ab != null){
            ab.setDisplayHomeAsUpEnabled(isShow);
            ab.setHomeButtonEnabled(isShow);
            if (isShow){
                ab.setHomeAsUpIndicator(R.drawable.ic_back);
            }
        }
    }

    /**
     * 判断当前activity是否已经嵌套了fragment
     * @return
     */
    public boolean hasMainFragment(){
        return getBackStackFragmentCount() > 0;
    }

    /**
     * 获取回退栈里fragment的数量
     * @return
     */
    public int getBackStackFragmentCount(){
        return mActivity.getSupportFragmentManager().getBackStackEntryCount();
    }

    /**
     * 弹出回退栈里最顶上的fragment
     * 如果栈里只有一个fragment的话,则在弹出的同时并结束掉当前的activity
     * @return
     */
    public boolean popTopFragmentBackStack(){
        final FragmentManager fm = mActivity.getSupportFragmentManager();
        final int backStackCount = fm.getBackStackEntryCount();
        if (backStackCount > 1){
            fm.popBackStack();
            return true;
        }
        return false;
    }

    /**
     * 弹出回退栈里的所有的fragment
     * @return
     */
    public boolean popEntireFragmentBackStack(){
        final FragmentManager fm = mActivity.getSupportFragmentManager();
        final int backStackCount = fm.getBackStackEntryCount();
        for (int i = 0; i < backStackCount; i++){
            fm.popBackStack();
        }
        return backStackCount > 0;
    }

    /**
     * 回退
     * @return
     */
    public void navigateUp(){
        final Intent intent = NavUtils.getParentActivityIntent(mActivity);
        if (intent != null) {
            NavUtils.navigateUpTo(mActivity,intent);
        }else {
            finishActivity();
        }
    }

    /**
     * 结束Activity
     */
    public void finishActivity(){
        mActivity.finish();
    }


    /**
     * 显示Fragment
     */
    public void showFragment(Fragment fragment){
        mActivity.getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(hasMainFragment() ? R.anim.right_in : 0, R.anim.left_out,
                        R.anim.left_in, R.anim.right_out)
                .replace(R.id.fragment_main,fragment)
                .addToBackStack(null)
                .commit();
    }

}
